//
//  VisitorLoginView.swift
//  sinaweibo
//
//  Created by mac on 15/11/9.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@objc protocol VisitorLoginViewDelegate :NSObjectProtocol {

    func visitorWillLogin()
    optional func visitorWillRegister()
}

class VisitorLoginView: UIView {

  
    
    weak var visitorDelegate : VisitorLoginViewDelegate?
    
    @objc func loginBtnDidClick() {
    
        visitorDelegate!.visitorWillLogin()
    }
    
    @objc func registerBtnDidClick() {
        //此处应该 用visitorWillRegister?  "?" 号 要不就蹦
        visitorDelegate!.visitorWillRegister?()
    }
  
    //MARK: 设置页面信息
    func setUIInfo(imageName: String?, title: String) {
        iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        } else {
            //让circleView 动起来
            startAnimation()
        }
    }
    
//    func setUIInfo (imagename : String?, title :String){
//        //        iconView.hidden = false
//        tipLabel.text = title
//        if imagename != nil {
//            
//            iconView.image = UIImage(named: imagename!)
//            //            bringSubviewToFront(circleView)
//            //            iconView.hidden = true
//            
//        }else {
//        
//            startAnimation()
//        }
//    }
    
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        //动画处于非活跃状态的时候  不移除图层
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey:nil)
    }
    
     override init(frame: CGRect) {
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//        super.init(coder: aDecoder)
    }
    
    
    private func setupUI() {
     
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        for v in subviews {
        
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //iconView的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //提示文字的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //登陆按钮 的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //注册按钮的 约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //backView的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView, "registerBtn":registerBtn]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
//        
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    
    //懒加载所有控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
    
    private lazy var loginBtn: UIButton = UIButton(title: "登陆", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())
    
}
